<?php

class User
{
	private $FirstName;
	private $LastName;
	private $Login;
	private $Password;
	private $Role;
	private $Email;
	private $Permissions;
	
	function __construct($login) {
		$this->Login = $login;
	}

	public function getRole()
	{
		return $this->Role;
	}
	public function getEmail()
	{
		return $this->Email;
	}
	
	public function getFirstName() {
		return $this->FirstName;
	}

	public function getLastName() {
		return $this->LastName;
	}
	
	public function getLogin() {
		return $this->Login;
	}
	
	public function getPassword() {
		return $this->Password;
	}
	
	public function setPassword($password) {
		return $this->Password = $password;
	}
	
	public function setEmail($email)
	{
		$this->Email = $email;
	}
	public function setRole($role)
	{
		$this->Role = $role;
	}

	public function setFirstName($firstName) {
		$this->FirstName = $firstName;
	}

	public function setLastName($lastName) {
		$this->LastName = $lastName;
	}
	
	public function setLogin($login) {
		$this->Login = $login;
	}
	
	public function setPermissions($permissions)
	{
		$this->Permissions = $permissions;
	}
	
	public function getPermissions()
	{
		return $this->Permissions;
	}
	public function verifEmail($filename){
		$xmlfile = simplexml_load_file($filename);
		$users = $xmlfile->xpath("/users/user");
		$bool=0;
		if(count($users)!=0)
		{
				while(list(,$user)=each($users))
			{
				if($user['email'] == $this->Email)
				{
					$bool=1;
				}
			}
		return $bool;
		}	
		else return 0;
	}
	public function verifUser($filename){
		$xmlfile = simplexml_load_file($filename);
		$users = $xmlfile->xpath("/users/user");
		$bool=0;
		if(count($users)!=0)
		{
				while(list(,$user)=each($users))
			{
				if($user['login'] == $this->Login)
				{
					$bool=1;
				}
			}
		return $bool;
		}	
		else return 0;
	}
	
	/**
	 * 
	 * @param string $permession the name of permission that the user may have.
	 * @param string $filename the file name witch the user may have permission on.
	 * @return boolean true if the user have the specified permission false othewise.
	 */
	public function hasPermission(Permission $permission,XML $xmlfile)
	{
		$hasPermission = false;
		$users = $xmlfile->get_xml_file()->xpath("/users/user");
		$permissionName = $permission->getName();
		$permissionFile = $permission->getFileName();
		$filepath = $permission->getFilepath();
		
		//if users exists in the file
		if(count($users)!=0)
		{
			while(list(,$user)=each($users))
			{
				//if user exists 
				if($user['login'] == $this->Login)
				{
					$permissions = $user->xpath("./permission[@name ='$permissionName' and @filename='$permissionFile']");
					if(count($permissions) == 1)
					{
						$hasPermission = true;
						break;
					}
				}
			}
		}
		else
		{
			echo "Aucun utilisateur dans le fichier ".$filename;
		}
		return $hasPermission;
	}
}